package student.mindmap.core.model;

import java.awt.*;
import java.util.*;
import java.util.List;
import javafx.scene.paint.Color;

public class MindMapNode {


    private Boolean layoutToLeft;

    public void setLayoutToLeft(Boolean layoutToLeft) {
        this.layoutToLeft = layoutToLeft;
    }

    public Boolean getLayoutToLeft() {
        return this.layoutToLeft;
    }

    // 节点唯一标识
    private final String id = UUID.randomUUID().toString();

    // 核心属性
    private String text;
    private MindMapNode parent;
    private List<MindMapNode> children = new ArrayList<>();

    // 图形属性
    private Point position = new Point(400, 300);  // 节点坐标
    private Dimension size = new Dimension(120, 40); // 默认节点大小
    private static Color defaultColor = Color.rgb(75, 255, 172);
    private static int defaultFontSize = 14;
    private static double defaultWidth = 80;



    public static Color getDefaultColor() { return defaultColor; }
    public static int getDefaultFontSize() { return defaultFontSize; }
    public static double getDefaultWidth() { return defaultWidth; }

    //------------------ 构造函数 -------------------
    public MindMapNode(String text) {
        this.text = text;
    }

    public static void setDefaultStyle(Color color, int fontSize, double width) {
        defaultColor = color;
        defaultFontSize = fontSize;
        defaultWidth = width;
    }

    // Override
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MindMapNode that = (MindMapNode) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    //------------------ 节点操作 -------------------
    // 添加子节点
    public void addChild(MindMapNode child) {
        child.setParent(this);
        children.add(child);
    }

    // 添加兄弟节点
    public void addSibling(MindMapNode sibling) {
        if (parent != null) {
            sibling.setParent(this.parent);
            parent.addChild(sibling);
        }
    }

    // 递归删除所有子节点
    public void removeAllChildren() {
        children.forEach(child -> {
            child.removeAllChildren();
            child.setParent(null);
        });
        children.clear();
    }

    //------------------ Getters/Setters -------------------
    public boolean hasSiblings() {
        return parent != null && parent.getChildren().size() > 1;
    }

    public List<MindMapNode> getSiblings() {
        if (parent == null) return Collections.emptyList();

        List<MindMapNode> siblings = new ArrayList<>();
        for (MindMapNode node : parent.getChildren()) {
            if (node != this) {
                siblings.add(node);
            }
        }
        return siblings;
    }

    public boolean isCenterTheme() {
        return "中心主题".equals(this.getText());
    }

    public MindMapNode findNodeByIndex(int index) {
        if (index >= 0 && index < children.size()) {
            return children.get(index);
        }
        return null;
    }

    public String getId() { return id; }
    public String getText() { return text; }
    public void setText(Object text) { this.text = text.toString(); }
    public MindMapNode getParent() { return parent; }
    public Point getPosition() { return position; }
    public Dimension getSize() { return size; }

    public void setParent(MindMapNode parent) { this.parent = parent; }
    public List<MindMapNode> getChildren() { return children; }
    public void setPosition(Point p) { this.position = p; }
    public void setSize(Dimension d) { this.size = d; }


}